// components/share-image/share-image.js
import painter from '../../utils/painterImage/image.js';
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isShow: {
      type: Boolean,
      observer(newVal,oldVal,changedPath){
        this.setData({isShowShare: newVal})
      }
    },
    toGoodsId: {
      type: Number,
      observer(newVal,oldVal,changedPath){
        this.setData({goodsId: newVal})
      }
    }
  },

  lifetimes: {
    ready () {
    },
  },


  /**
   * 组件的初始数据
   */
  data: {
    goodsId: '',
    isShowShare: false,
    actions: [
      {name: '发送给朋友',openType: 'share'},
      {name: '生成海报'}
      ],
    template: {},
    shareImage: '',
    isImage: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    touchmove(){},

    onSelect(event){
      if(event.detail.name == this.data.actions[0].name){//发送给朋友

      } else {//生成海报
        this.getReq();
      }
    },

    //分享接口
    getReq(){
      this.setData({shareImage: ''})
      this.isImageMonitor();
      let user = app.getUser();
      let shop = app.getShop();
      wx.showLoading({
        mask: true,
        title: '加载中'
      })
      let _this = this;
      let path = app.globalData.deployUrl + '/subIndex/index/good-item/good-item/?heUpshopTid=' + user.upshopTid + '&heCentreId=' + user.centreId + '&heGoodsId=%2Fitems%2Fdetail%2F' + _this.data.goodsId;
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/shop/products/shareData',
        data: {
          centreId: user.centreId,
          shopId: shop.upshopTid,
          teamId: shop.teamid,
          productId: _this.data.goodsId,
          url: path,
        }
      }).then(res =>{
        let data = res.data.data;
        this.setData({
          template: new painter().palette('https://o.rjkj7.com/data/upload/xcximages/yantong/canvas_bg.png',data.thumbSquare,data.qrUrl,data.title,data.marketprice,data.unit,data.productprice,data.producttotal),
        })
      })

    },

    onClose() {
      this.setData({
        isShowShare: false,
      });
      this.triggerEvent('isShowShareSuffer', false);
    },
    isImageMonitor() {
      let _this = this;
      _this.setData({
        isImage: !_this.data.isImage,
      })
      if (_this.data.isImage == false) _this.setData({shareImage: ''})
    },



    onImgOK(e) {
      this.setData({
        shareImage: e.detail.path
      })
      wx.hideLoading()
    },
  }
})
